package match;

import com.ocl.www.utils.ResourceBundleUtils;
import org.apache.commons.lang3.StringUtils;


public class Matching {
	private static String featuresPath = ResourceBundleUtils.getProperty("image.match.features.path");// 获取特征值文件
//	private static String imgPath = ResourceBundleUtils.getProperty("image.match.img.path");//路径
//	private static String picformat = ResourceBundleUtils.getProperty("image.match.pic.format");//后缀
	//1:加载图片解析库
	public static void test() {
		
		try {
			System.out.println("start load so...."+Matching.class.getResource("/").getPath());
			System.load(Matching.class.getResource("/").getPath() + "libMatching.so");
//			System.load(Matching.class.getResource("/").getPath() + "libFileManagement.so");
//			System.load(Matching.class.getResource("/").getPath() + "libExtracFeature.so");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("successed load so....");
	}

	static boolean isLoadFeatures = false;

	
	private native static LoadFeatureResult LoadFeatures(String strPath);

	private native static MatchingResult[] MatchImage(int[] pImgData, int nWidth, int nHeight, int nTopN,
			boolean bShowImage);

	/**
	 * 调用接口运行后，会在图像所在目录下生成相应的特征文件，特征文件命名规则为：“图像名_Feat.yaml”，如1_Feat.yaml。
	 * @param strPath
	 * @return
	 */
//	private native static FeatureExtractionResult ExtractFeature(String strPath);
//	
//	
//	
//	
//	public static boolean extractFeature(){
//		
//		System.out.println("path" + featuresPath);
//		FeatureExtractionResult loadResult = Matching.ExtractFeature(featuresPath);
//		System.out.println("LoadFeatureExtractionResult...");
//		if (loadResult.m_nFlag == 1){
//			//isLoadFeatures = true;
//			return true;
//		}
//		return false;
//	}
	
	/**
	 * 调用接口运行后，模块会将文件夹内的文件数量以及文件路径名写在strSavePath内。
	 * @return
	 */
//	private native static FileManagementResult ManageFile(String strDir, String strSavePath, String strFormat);
//	
//	public static boolean manageFile(){
//		System.out.println("path" + featuresPath);
//		FileManagementResult loadResult = Matching.ManageFile(imgPath,featuresPath,picformat);
//		System.out.println("LoadFeatureExtractionResult...");
//		if (loadResult.m_nFlag == 1){
//			//isLoadFeatures = true;
//			return true;
//		}
//		return false;
//		
//	}
	
	public static boolean loadFeatures(){
		System.out.println("path" + featuresPath);
		LoadFeatureResult loadResult = Matching.LoadFeatures(featuresPath);
		System.out.println("LoadFeatures...");
		if (loadResult.m_nFlag == 1){
			isLoadFeatures = true;
			return true;
		}
		return false;
	}
	
	//2：依据三个参数执行匹配，返回匹配的结果集
	public static MatchingResult[] matching(int[] pixel, int width, int height) {
		if (!isLoadFeatures) {
			loadFeatures();
		}
		System.out.println("matching....!!!"+width + "-"+height);
//		MatchingResult[] results = Matching.MatchImage(pixel, width, height, 3, false);
		MatchingResult[] results = Matching.MatchImage(pixel, width, height, 3, true);
		MatchingResult [] resultBs = new MatchingResult[3];
		if(null == results){
			return null;
		}
		for(int i = 0; i < results.length; i++){
			 System.out.println(results[i].m_strName + " : " + results[i].m_nSimilary);
			 if(StringUtils.isEmpty(results[i].m_strName) || "null" == results[i].m_strName || results[i].m_nSimilary < 20){
				 continue;
			 }
			 MatchingResult resultB = new MatchingResult();
			 resultB.setmNFlag(results[i].m_nFlag);
			 resultB.setmNSimilary(results[i].m_nSimilary);
			 resultB.setmStrName(results[i].m_strName);
			 resultBs[i] = resultB;
		}
		return resultBs;
	}

	
}
